package com.zyfdroid.bigdialerkit.dao;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MissingCall {

    /**
     * _id in content provider database
     */
    public int _id;


    public String displayName;

    public String phoneNumber;

    public long callTime;


    public MissingCall(int _id, String displayName, String phoneNumber, long callTime) {
        this._id = _id;
        this.displayName = displayName;
        this.phoneNumber = phoneNumber;
        this.callTime = callTime;
    }

    private static String getContactNameFromNumber(Context context, String phoneNumber) {
        String contactName = null;
        Cursor cursor = null;
        try {
            // 使用PhoneNumberUtils.formatNumber将电话号码格式化为国际格式
            String orignalNumber = phoneNumber.trim();
            String formattedNumber = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().getCountry());

            // 查询Contacts表
            cursor = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
                    new String[]{orignalNumber,formattedNumber},
                    null
            );

            if (cursor != null && cursor.moveToFirst()) {
                contactName = cursor.getString(0); // 获取联系人姓名
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return contactName;
    }
    public static List<MissingCall> LoadData(Context mContext) {
        List<MissingCall> result = new ArrayList<>();

        int callLogsAtMost = 9; // 最多显示来电数量
        long oneWeekAgo = System.currentTimeMillis() - (7L * 24 * 60 * 60 * 1000); // 计算一周前的时间戳

        Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI,
                new String[]{CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.DATE},
                CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE + " AND " + CallLog.Calls.DATE + " > " + oneWeekAgo,
                null, CallLog.Calls.DATE + " DESC");

        if (cursor != null) {
            while (cursor.moveToNext() && result.size() <= callLogsAtMost) {
                int _id = cursor.getInt(0);
                String number = cursor.getString(1);
                String name = cursor.getString(2);
                long date = cursor.getLong(3);
                if(TextUtils.isEmpty(name)){
                    name =  getContactNameFromNumber(mContext, number);
                }
                result.add(new MissingCall(_id, name, number, date));

            }
            cursor.close();
        }
        return result;
    }

}
